{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 划分训练和测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "folder = '../TableGenerations/dataset04/'\n",
    "fns = [f for f in os.listdir(folder + 'imgs/')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "total = len(fns)\n",
    "test_ratio = 0.2\n",
    "test_size = int(total * test_ratio)\n",
    "test_ids = np.random.choice(np.arange(0, total), test_size, replace=False)\n",
    "train_ids = list(set(np.arange(0, total)) - set(test_ids))\n",
    "# test_f_names = [\"{0}.png\".format(one).rjust(10, '0') for one in test_ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_folder = folder \n",
    "target_train_folder = source_folder + 'train/'\n",
    "target_test_folder = source_folder + 'test/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_imgs_path = os.path.join(target_train_folder, 'imgs')\n",
    "shutil.rmtree(train_imgs_path)\n",
    "os.mkdir(train_imgs_path)\n",
    "\n",
    "train_masks_path = os.path.join(target_train_folder, 'masks')\n",
    "shutil.rmtree(train_masks_path)\n",
    "os.mkdir(train_masks_path)\n",
    "\n",
    "for i in train_ids:\n",
    "    f_n = fns[i]\n",
    "    shutil.copy(os.path.join(source_folder, 'imgs', f_n),os.path.join(target_train_folder, 'imgs', f_n))\n",
    "    shutil.copy(os.path.join(source_folder, 'masks', f_n),os.path.join(target_train_folder, 'masks', f_n))\n",
    "\n",
    "test_imgs_path = os.path.join(target_test_folder, 'imgs')\n",
    "shutil.rmtree(test_imgs_path)\n",
    "os.mkdir(test_imgs_path)\n",
    "\n",
    "test_masks_path = os.path.join(target_test_folder, 'masks')\n",
    "shutil.rmtree(test_masks_path)\n",
    "os.mkdir(test_masks_path)\n",
    "\n",
    "for i in test_ids:\n",
    "    f_n = fns[i]\n",
    "    shutil.copy(os.path.join(source_folder, 'imgs', f_n),os.path.join(target_test_folder, 'imgs', f_n))\n",
    "    shutil.copy(os.path.join(source_folder, 'masks', f_n),os.path.join(target_test_folder, 'masks', f_n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch-py3",
   "language": "python",
   "name": "pytorch-py3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
